package com.face.console.tools;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;


/**
 * Description: 返回实体封装类
 * <p>
 * Created by Andy_Lau on 2020/12/1 0:14
 */
@Data
public class R {



    private static final int SUCCESS_CODE = 0;
    private static final int ERROR_CODE = 1;
    private static final String SUCCESS_R = "操作成功！";
    private static final String ERROR_R = "操作失败！";


    private int code;
    private String msg;
    private Object data;

    public R() {}

    public static R success()
    {
        R r = new R();

        r.setCode(SUCCESS_CODE);
        r.setMsg(SUCCESS_R);
        return r;
    }

    public static R success(Object data)
    {
        R r = new R();
        r.setCode(SUCCESS_CODE);
        r.setMsg(SUCCESS_R);
        r.setData(data);
        return r;
    }

    public static R success(String msg)
    {
        R r = new R();
        r.setCode(SUCCESS_CODE);
        r.setMsg(msg);
        return r;
    }


        public static R error()
        {
            R r = new R();
            r.setCode(ERROR_CODE);
            r.setMsg(ERROR_R);
            return r;
        }


    public static R error(Error error, String tip)
    {
        if (StringUtils.isNotBlank(tip))
        {
            tip = "，" + tip;
        }
        else
        {
            tip = "";
        }
        return error(error.getCode(), error.getErrMsg() + tip);
    }

    public static R error(Error error)
    {
        return error(error, null);
    }


    public static R error(int code, String msg)
    {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
}
